package heven.holt.wan.android.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.navigation.findNavController
import dagger.hilt.android.AndroidEntryPoint
import heven.holt.library.base.BaseFragment
import heven.holt.wan.android.MainFragmentDirections
import heven.holt.wan.android.adapter.NavigationLinkMenuAdapter
import heven.holt.wan.android.databinding.FragmentNavigationLinkBinding
import heven.holt.wan.android.databinding.ItemNavigationLinkContentBinding
import heven.holt.wan.android.model.NavigationModel
import heven.holt.wan.android.viewmodel.NavigationViewModel

@AndroidEntryPoint
class NavigationLinkFragment : BaseFragment<FragmentNavigationLinkBinding>() {
    private val navigationViewModel: NavigationViewModel by viewModels()

    override fun bindViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): FragmentNavigationLinkBinding =
        FragmentNavigationLinkBinding.inflate(inflater, container, false)

    override fun initView() {
        binding.recyclerView.itemAnimator = null
    }

    override fun initData() {
        navigationViewModel.navigationListLiveData.observe(viewLifecycleOwner) { navigationList ->
            val currentSelect = navigationList.indexOfFirst { it.isSelect }
            val menuAdapter = NavigationLinkMenuAdapter(navigationList, currentSelect)
            menuAdapter.onItemClickListener = { oldPosition, newPosition ->
                notifyItemByPosition(menuAdapter, navigationList, oldPosition, false)
                notifyItemByPosition(menuAdapter, navigationList, newPosition, true)
                val cid = navigationList[newPosition].cid
                cid?.let { navigationViewModel.setCurrentCid(it) }
            }
            binding.recyclerView.adapter = menuAdapter
        }

        navigationViewModel.navigationContentListLiveData.observe(viewLifecycleOwner) { articles ->
            binding.flexBoxLayout.removeAllViews()
            articles.forEach { articleModel ->
                val itemBinding = ItemNavigationLinkContentBinding.inflate(
                    layoutInflater,
                    binding.flexBoxLayout,
                    false
                )
                itemBinding.root.text = articleModel.title
                itemBinding.root.setOnClickListener { view ->
                    articleModel.link?.let {
                        val direction =
                            MainFragmentDirections.actionGlobalWebViewFragment(it)
                        view.findNavController().navigate(direction)
                    }
                }
                binding.flexBoxLayout.addView(itemBinding.root)
            }

        }
    }

    private fun notifyItemByPosition(
        menuAdapter: NavigationLinkMenuAdapter,
        navigationList: List<NavigationModel>,
        position: Int,
        isSelect: Boolean
    ) {
        navigationList[position].isSelect = isSelect
        menuAdapter.notifyItemChanged(position)
    }
}